package com.zygh.policeasset.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.zygh.policeasset.component.CommonResult;
import com.zygh.policeasset.pojo.ProcessRecord;
import com.zygh.policeasset.pojo.Purchase;
import com.zygh.policeasset.service.ProcessRecordSerivce;
import com.zygh.policeasset.service.ProjectService;
import com.zygh.policeasset.service.PurchaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping({"/purchase"})
@Api(tags = {"采购资产模块"})
@RestController
/* loaded from: police-asset-2.0-0.0.1-SNAPSHOT.jar:BOOT-INF/classes/com/zygh/policeasset/controller/PurchaseController.class */
public class PurchaseController {
    @Autowired
    PurchaseService purchaseService;
    @Autowired
    ProcessRecordSerivce processRecordSerivce;
    @Autowired
    ProjectService projectService;

    @RequestMapping(value = {"/status"}, method = {RequestMethod.GET})
    @ApiImplicitParams({@ApiImplicitParam(name = "status", value = "0是未提交、1是已提交、2是已通过、3是已拒绝", required = true, paramType = "query"), @ApiImplicitParam(name = "pageNum", value = "页码", required = true, paramType = "query"), @ApiImplicitParam(name = "pageSize", value = "页数", required = true, paramType = "query")})
    @ApiOperation("根据状态获取采购立项分页列表")
    public Object getByStatus(@RequestParam(value = "status", required = false) Integer status,
                              @RequestParam(value = "company", required = false) Integer company,
                              @RequestParam(value = "name", required = false) String name,
                              @RequestParam(value = "dataLabelsId", required = false) Integer dataLabelsId,
                              @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum,
                              @RequestParam(defaultValue = "10", value = "pageSize") Integer pageSize,
                              @RequestHeader(value = "findAll", required = false) boolean findAll,
                              @RequestHeader(value = "userId", required = false) Integer userId,
                              @RequestParam(value = "combined", required = false) Double combined) {
        if (findAll) {
            return this.purchaseService.findByStatus(status, dataLabelsId, name, pageNum, pageSize, null, company, combined);
        }
        return this.purchaseService.findByStatus(status, dataLabelsId, name, pageNum, pageSize, userId, company, combined);
    }

    @RequestMapping(value = {"/findById"}, method = {RequestMethod.GET})
    @ApiOperation("根据id查询")
    public Object findById(@RequestParam(value = "id", required = false) Integer id) {
        return this.purchaseService.findbyId(id);
    }

    @RequestMapping(value = {"/statusExcel"}, method = {RequestMethod.GET})
    @ApiImplicitParams({@ApiImplicitParam(name = "status", value = "0是未提交、1是已提交、2是已通过、3是已拒绝", required = true, paramType = "query"), @ApiImplicitParam(name = "pageNum", value = "页码", required = true, paramType = "query"), @ApiImplicitParam(name = "pageSize", value = "页数", required = true, paramType = "query")})
    @ApiOperation("根据状态获取采购立项分页列表")
    public Object statusExcel(@RequestParam(value = "status", required = false) int status, @RequestParam(value = "companyId", required = false) Integer companyId, @RequestParam(value = "name", required = false) String name, @RequestParam(value = "dataLabelsId", required = false) Integer dataLabelsId, @RequestParam(value = "userId", required = false) Integer userId, @RequestHeader(value = "findAll", required = false) boolean findAll, @RequestHeader(value = "unitId", required = false) Integer unitId, HttpServletResponse response) {
        if (findAll) {
            return this.purchaseService.findByStatusExcel(Integer.valueOf(status), dataLabelsId, name, response, userId, companyId);
        }
        return this.purchaseService.findByStatusExcel(Integer.valueOf(status), dataLabelsId, name, response, userId, unitId);
    }

    @RequestMapping(value = {"/findoneall"}, method = {RequestMethod.GET})
    @ApiOperation("获取某项目下所有采购资产列表")
    public Object getAll(@RequestParam String projectNo, @RequestParam String purchaseName, @RequestParam Integer assetsClassificationId, @RequestParam(defaultValue = "1", value = "pageNum") int pageNum, @RequestParam(defaultValue = "10", value = "pageSize") int pageSize) {
        return this.purchaseService.findOneAll(projectNo, pageNum, pageSize, purchaseName, assetsClassificationId);
    }

    @RequestMapping(value = {"/findbyid"}, method = {RequestMethod.GET})
    @ApiOperation("获取某采购资产数据")
    public Object findById(@RequestParam int id) {
        return this.purchaseService.findById(id);
    }

    @RequestMapping(value = {"/add"}, method = {RequestMethod.POST})
    @ApiOperation("添加/修改采购资产")
    public Object add(@RequestBody Purchase purchase) {
        String add = purchase.getAdd();
        if (!(purchase.getGetDatetime() == null || purchase.getGetDatetime() == "")) {
            purchase.setGetDatetime(purchase.getGetDatetime().substring(0, 10));
        }
        if (this.purchaseService.add(purchase, add).intValue() > 0) {
            return new CommonResult().success(purchase.getId());
        }
        return new CommonResult().validateFailed("添加错误");
    }

    @RequestMapping(value = {"/remove"}, method = {RequestMethod.GET})
    @ApiOperation("删除资产")
    public Object remove(@RequestParam int id) {
        if (StrUtil.isEmpty(String.valueOf(id))) {
            return new CommonResult().validateFailed("id不能为空");
        }
        if (this.purchaseService.deleteById(id).intValue() > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("删除错误");
    }

    @RequestMapping(value = {"/submit"}, method = {RequestMethod.POST})
    @ApiImplicitParams({@ApiImplicitParam(name = "projectNo", value = "项目编号", required = true, paramType = "query"), @ApiImplicitParam(name = "projectId", value = "项目编号", required = true, paramType = "query"), @ApiImplicitParam(name = "submitId", value = "提交人id", required = true, paramType = "query")})
    @ApiOperation("全部提交")
    @Transactional
    public Object submit(@RequestBody JSONObject jsonObject) {
        String projectNo = jsonObject.get("projectNo").toString();
        int projectId = Integer.parseInt(jsonObject.get("projectId").toString());
        String submitId = jsonObject.get("submitId").toString();
        if (this.purchaseService.findOneAll(projectNo, 1, 10, null, null).getSize() == 0) {
            return new CommonResult().validateFailed("该项目未添加资产");
        }
        int count = this.purchaseService.submit(projectNo).intValue();
        ProcessRecord oldprocessRecord = new ProcessRecord();
        oldprocessRecord.setProjectId(projectId);
        oldprocessRecord.setProcessName("project");
        ProcessRecord oldprocessRecord2 = this.processRecordSerivce.findOne(oldprocessRecord);
        oldprocessRecord2.setStatus(4);
        this.processRecordSerivce.updateStatus(oldprocessRecord2).intValue();
        ProcessRecord newprocessRecord = new ProcessRecord();
        newprocessRecord.setProjectId(projectId);
        newprocessRecord.setProcessName("cg");
        newprocessRecord.setStatus(4);
        newprocessRecord.setProcessTime(DateUtil.now());
        newprocessRecord.setSubmitId(submitId);
        int newallflow = this.processRecordSerivce.add(newprocessRecord).intValue();
        if (count <= 0 || newallflow <= 0) {
            return new CommonResult().validateFailed("提交失败");
        }
        return new CommonResult().success();
    }

    @RequestMapping(value = {"/againsubmit"}, method = {RequestMethod.POST})
    @ApiImplicitParams({@ApiImplicitParam(name = "projectNo", value = "项目编号", required = true, paramType = "query"), @ApiImplicitParam(name = "projectId", value = "项目编号", required = true, paramType = "query"), @ApiImplicitParam(name = "submitId", value = "提交人id", required = true, paramType = "query")})
    @ApiOperation("再次提交")
    public Object againSubmit(@RequestBody JSONObject jsonObject) {
        String projectNo = jsonObject.get("projectNo").toString();
        int projectId = Integer.parseInt(jsonObject.get("projectId").toString());
        String submitId = jsonObject.get("submitId").toString();
        if (this.purchaseService.findOneAll(projectNo, 1, 10, null, null).getSize() == 0) {
            return new CommonResult().validateFailed("该项目未添加资产");
        }
        int count = this.purchaseService.submit(projectNo).intValue();
        ProcessRecord processRecord = new ProcessRecord();
        processRecord.setProjectId(projectId);
        processRecord.setProcessName("cg");
        processRecord.setProcessTime(DateUtil.now());
        processRecord.setStatus(4);
        processRecord.setSubmitId(submitId);
        int allflow = this.processRecordSerivce.updateStatus(processRecord).intValue();
        if (count <= 0 || allflow <= 0) {
            return new CommonResult().validateFailed("提交失败");
        }
        return new CommonResult().success();
    }

    @RequestMapping(value = {"app"}, method = {RequestMethod.POST})
    @ApiOperation("审批")
    public Object app(@RequestBody Purchase purchase) {
        if (this.purchaseService.app(purchase, purchase.getResult()).intValue() > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("审批失败");
    }

    @RequestMapping(value = {"/updateStartProvisionDate"}, method = {RequestMethod.POST})
    @ApiImplicitParams({@ApiImplicitParam(name = "startProvisionDate", value = "折旧开始日期", required = true, paramType = "query"), @ApiImplicitParam(name = "buildNum", value = "项目编号", required = true, paramType = "query")})
    @ApiOperation("修改开始计提折旧日期")
    public Object updateStartProvisionDate(@RequestBody JSONObject json) {
        String startProvisionDate = json.getString("startProvisionDate");
        String[] buildNums = json.getString("buildNum").split(",");
        int count2 = 0;
        for (String str : buildNums) {
            if (this.purchaseService.findByBuildNum(str).intValue() == 0) {
                return new CommonResult().validateFailed("当前项目无资产，请录入资产");
            }
            if (this.projectService.updateSPD(str, startProvisionDate, null).intValue() == 0) {
                return new CommonResult().validateFailed("修改项目信息失败");
            }
            count2 = this.purchaseService.updateStartProvisionDate(startProvisionDate, str).intValue();
        }
        if (count2 > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("修改失败");
    }
}